/**************************************************************************
 *
 * Copyright (C) 2007 University of California, Los Angeles.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 *************************************************************************/

/**
 * 
 */
package edu.ucla.cs.typecast.cache2;

import java.io.IOException;
import java.net.InetAddress;
import java.util.Collection;
import java.util.Hashtable;

import edu.ucla.cs.typecast.net.EndPoint;
import edu.ucla.cs.typecast.net.TypeCastConstants;
import edu.ucla.cs.typecast.scope.NetScope;

/**
 *
 * @date Aug 20, 2007
 */
public class CacheManager {
	private static CacheManager instance = new CacheManager(); 
	public static CacheManager getInstance() { 
		return instance; 
	}
	
	Hashtable<EndPoint, CacheRequestor> requestors = new Hashtable<EndPoint, CacheRequestor>(); 
	Hashtable<EndPoint, CacheAccessControl> controllers = new Hashtable<EndPoint, CacheAccessControl>(); 
	
	private CacheManager() { }

	public synchronized  CacheRequestor getCacheRequestor(EndPoint endPoint) throws IOException { 
		CacheRequestor requestor = requestors.get(endPoint); 
		if (requestor == null) { 
			requestor = new CacheRequestor(endPoint); 
			requestors.put(endPoint, requestor); 			
		}
		return requestor; 
	}
	
	public synchronized void enableCacheService() throws IOException { 
	    this.enableCacheService(new EndPoint(InetAddress.getByName(TypeCastConstants.DEFAULT_RMI_ADDRESS), TypeCastConstants.DEFAULT_RMI_PORT));
	}
	
	public synchronized void enableCacheService(EndPoint endPoint) throws IOException { 
		CacheAccessControl controller = controllers.get(endPoint); 
		if (controller == null) { 
			controller = new CacheAccessControl(endPoint); 
			controllers.put(endPoint, controller); 
		}
	}
	
	public void disableCacheService(EndPoint endPoint) { 
		//@TODO
	}
	
    public Collection<CachedObject> getCachedObjects() { 
        try { 
            return getCachedObjects(EndPoint.DEFAULT_RMI_ENDPOINT); 
        } catch(Throwable t) { 
            throw new RuntimeException(t); 
        }        
    }
    
	public Collection<CachedObject> getCachedObjects(EndPoint endPoint) { 
		CacheAccessControl controller = controllers.get(endPoint); 
		if (controller != null) { 
			return controller.getCachedObjects(); 
		}
		return null; 
	}
}
